#include "./../inc/main.h"

int save_map()
{
    FILE *fp;
    char load_path[50] = "./res/loadMap";
    ensureDirectoryExists(load_path); // 确保目录存在
    strcat(load_path, map_path[map_index]);
    fp = fopen(load_path, "w");
    if (fp == NULL)
    {
        perror("open file error");
        return 0;
    }

    for (int row = 0; row < 15; row++)
    {
        for (int col = 0; col < 20; col++)
        {
            fprintf(fp, "%d", curr_map->map[row][col]);
            if (col < 19)
            {
                fprintf(fp, ",");
            }
        }
        fprintf(fp, "\n"); // 每行末尾换行
    }
    // printf("地图保存成功!");
    fclose(fp);
    return 1;
}

map_p load_map()
{
    FILE *fp;
    int status = 0; // 记录是否为新玩家    0为旧玩家，1为新玩家
    char load_path[50] = "./res/loadMap";
    strcat(load_path, map_path[map_index]);
    fp = fopen(load_path, "r");
    if (!fp)
    {
        status = 1; // 记录为新玩家
        char original_path[50] = "./res/originalMap";
        strcat(original_path, map_path[map_index]);
        fp = fopen(original_path, "r");
        if (!fp)
        {
            perror("打开地图文件失败");
            return NULL;
        }
    }

    map_p map = (map_p)malloc(sizeof(map_t));

    strcpy(map->map_name, "未命名地图");
    map->map_width = 20;
    map->map_height = 15;

    // 直接读取整数，忽略逗号
    for (int row = 0; row < 15; row++)
    {
        for (int col = 0; col < 20; col++)
        {
            // 读取数字，跳过逗号
            int value;
            if (fscanf(fp, "%d,", &value) == 1)
            {
                map->map[row][col] = value;
            }
            else
            {
                map->map[row][col] = 0;
            }
        }
    }
    if (status == 1)
    {
        map->map[1][1] = 2;
    }
    fclose(fp);

    return map;
}

int save_per()
{
    FILE *fp;
    char save_path[50] = "./res/per";
    ensureDirectoryExists(save_path); // 确保目录存在
    strcat(save_path, "/per.bin");
    fp = fopen(save_path, "wb");
    if (fp == NULL)
    {
        perror("open file error");
        return 0;
    }
    // 写入文件头标识
    const char header[] = "PERSAVE";
    fwrite(header, sizeof(header) - 1, 1, fp);

    // 写入数据
    fwrite(per, sizeof(per_t), 1, fp);
    // printf("角色保存成功!\n");
    fclose(fp);
    return 1;
}

// 加载人物数据，若无数据返还0
int load_per()
{
    per = (per_p)malloc(sizeof(per_t));  // 给全局变量 per 分配空间

    FILE *fp;
    fp = fopen("./res/per/per.bin", "rb");
    if (fp == NULL)
    {
        per = perInit();
        save_per();
        return 0;
    }

    // 检查文件标识符
    char magic[8] = {0}; // 初始化为0
    if (fread(magic, sizeof(char), 7, fp) != 7 ||
        strncmp(magic, "PERSAVE", 7) != 0)
    {
        printf("错误：不是有效的二进制文件格式\n");
        per = perInit();
        save_per();
        fclose(fp);
        return 0;
    }

    fread(per, sizeof(per_t), 1, fp);

    fclose(fp);
    return 1;
}

// 检查并创建路径
void ensureDirectoryExists(const char *path)
{
    struct stat st;
    if (stat(path, &st) != 0)
    {
        // 目录不存在，尝试创建
        if (MAKE_DIR(path) != 0 && errno != EEXIST)
        {
            perror("创建目录失败");
        }
    }
}

// 创建目录的函数
int createDirectory(const char *path)
{
#ifdef _WIN32
    return _mkdir(path);
#else
    return mkdir(path, 0777); // Linux
#endif
}

void save()
{
    if (save_map() == 1 && save_per() == 1)
        printf("数据保存成功!\n");
}